package com.news.news.web;

import com.github.pagehelper.PageInfo;
import com.news.news.modle.Menu;
import com.news.news.modle.NewsType;
import com.news.news.modle.Role;
import com.news.news.service.NewsTypeService;
import com.news.news.service.RoleService;
import com.news.news.util.CommonResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Created by 罗铠威 on 2023/8/29 8:19
 * type:
 */
@RestController
@RequestMapping(path = "/admin/NewsTypeCtl")
public class NewsTypeController {
    @Resource
    NewsTypeService newsTypeService;

    /**
     * 获取新闻类型数据列表
     * @return
     */
    @RequestMapping(value = "/list")
    public CommonResult list(@RequestBody NewsType newsType) {
        PageInfo<NewsType> newsTypePageInfo = newsTypeService.NewsTypeList(newsType);//实现分页功能
        CommonResult commonResult=new CommonResult(200,newsTypePageInfo,"成功");

        return commonResult;
    }
    /**
     * 保存新增新闻类型信息
     * @return
     */
    @RequestMapping(value = "/saveNewsType")
    public CommonResult saveNewsType(@RequestBody NewsType newsType, @RequestHeader("adminToken") String token) {
        if (newsType.getId()!= null) {//【修改】
            newsTypeService.updateNewsType(newsType,token);
        } else {//【新增】
            newsTypeService.savenewsType(newsType,token);
        }
        CommonResult commonResult = new CommonResult(200, null, "成功");

        return commonResult;
    }

    /**
     * 修改新闻类型状态
     * @param newsType
     * @return
     */
    @RequestMapping(value = "/UpdateNewsTypeState")
    public CommonResult UpdateNewsTypeState(@RequestBody NewsType newsType) {

            newsTypeService.UpdateNewsTypeState(newsType);
        CommonResult commonResult = new CommonResult(200, null, "修改状态成功");

        return commonResult;
    }

    /**
     * 【编辑前显示数据】
     * @param id 新闻类型的ID，通过它来获取信息返回前端
     * @return
     */
    @RequestMapping(value = "/findNewsTypeById")
    public CommonResult findNewsTypeById(Integer id) {

        NewsType newsType=newsTypeService.findNewsTypeById(id);
        CommonResult commonResult = new CommonResult(200, newsType, "修改状态成功");

        return commonResult;
    }


    /**
     * 【删除数据】
     * @param id 新闻类型的ID，通过它来删除数据
     * @return
     */
    @RequestMapping(value = "/deleteNewsTypeById")
    public CommonResult deleteNewsTypeById(Integer id) {

        newsTypeService.deleteNewsTypeById(id);
        CommonResult commonResult = new CommonResult(200, null, "修改状态成功");

        return commonResult;
    }

}
